package com.seocoo.worker.order;

import android.app.Activity;
import android.arch.lifecycle.Lifecycle;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.seocoo.worker.R;
import com.seocoo.worker.base.RxOrderRefrect;
import com.seocoo.worker.base.http.BasePageRsp;
import com.seocoo.worker.base.refresh.SwipeRefreshMoreLayout;
import com.seocoo.worker.order.adapter.OrderMaintainAdapter;
import com.seocoo.worker.order.entity.RspOrderMaintain;
import com.seocoo.worker.order.entity.RxCancelInstallSuccess;
import com.seocoo.worker.order.model.OrderMaintainDetailModel;
import com.seocoo.worker.order.model.OrderMaintainModel;
import com.seocoo.worker.order.req.ReqOrderMaintain;
import com.sfh.lib.event.BusEvent;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMFragment;

import java.util.List;

/**
 * author : Ssp
 * date   : 2020/2/23 16:25
 * desc   : 维修订单列表
 */
public class OrderMaintainFragment extends MVVMFragment implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener, SwipeRefreshMoreLayout.OnLoadingListener {

    private OrderMaintainAdapter mAdapter = new OrderMaintainAdapter();
    private SwipeRefreshMoreLayout swipe_refresh_more_layout;
    private String orderType;
    private String takeType;

    public static OrderMaintainFragment newInstance(String orderType, String takeType) {
        OrderMaintainFragment fragment = new OrderMaintainFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderType", orderType);
        bundle.putString("takeType", takeType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayout() {
        return R.layout.order_maintain_fragment;
    }

    @Override
    public void initData(View view) {
        orderType = getArguments().getString("orderType");
        takeType = getArguments().getString("takeType");
        swipe_refresh_more_layout = view.findViewById(R.id.swipe_refresh_more_layout);
        swipe_refresh_more_layout.setAdapter(mAdapter, true);
        swipe_refresh_more_layout.setOnItemChildClickListener(this);
        swipe_refresh_more_layout.setOnItemClickListener(this);
        swipe_refresh_more_layout.setOnLoadingListener(this);
        swipe_refresh_more_layout.addItemDecorationSize(17);
        reFreshData();
    }

    public void reFreshData() {
        swipe_refresh_more_layout.onAutoRefresh();
    }

    @LiveDataMatch
    public void onQuerySuccess(BasePageRsp<RspOrderMaintain> data) {

        String types = "";
        if (TextUtils.equals("2", takeType)) {
            types = "线下交易";
        } else if (TextUtils.equals("1", takeType)) {
            types = "平台指派";
        }
        mAdapter.setTakeType(types);
        swipe_refresh_more_layout.onRefreshCompale(data);
    }

    @LiveDataMatch
    public void onTakeMaintainOrderSuccess() {
        swipe_refresh_more_layout.onAutoRefresh();
    }

    @LiveDataMatch
    public void onQueryError() {
        swipe_refresh_more_layout.onLoadMoreFail();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (view.getId() == R.id.tv_order_receiving) {
            RspOrderMaintain o = (RspOrderMaintain) adapter.getData().get(position);
            getViewModel(OrderMaintainDetailModel.class).takeMaintainOrder(o.getMaintainId());
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List data = adapter.getData();
        if (data.isEmpty()) {
            return;
        }
        String types = "";
        if (TextUtils.equals("2", takeType)) {
            types = "线下交易";
        } else if (TextUtils.equals("1", takeType)) {
            types = "平台指派";
        }
        RspOrderMaintain item = (RspOrderMaintain) data.get(position);
        OrderMaintainDetailActivity.start(this, item.getMaintainId(),types);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && requestCode == Activity.RESULT_OK) {
            swipe_refresh_more_layout.onAutoRefresh();
        }
    }

    @Override
    public void onLoadMoreUI(int nowPage) {
        getViewModel(OrderMaintainModel.class).refreshData(new ReqOrderMaintain(nowPage, orderType, takeType));
    }

    @BusEvent(from = "推送消息刷新列表")
    public void refresh(RxOrderRefrect refrect) {
        if (this.isVisible() && Lifecycle.State.STARTED == this.getLifecycle().getCurrentState()) {
            swipe_refresh_more_layout.onAutoRefresh();
        }
    }

    @BusEvent(from = "接单成功")
    public void refresh(RxCancelInstallSuccess result) {
        if (result.isCancelSuccess()) {
            swipe_refresh_more_layout.onAutoRefresh();
        }
    }
}
